package com.zhuss.car.processor;

import java.util.Date;

import com.zhuss.car.processor.cache.CarCache;
import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.car.car.DeleteCarBo;
import com.zhuss.bean.vo.car.car.DetailCarVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.car.db.dao.ext.CarDao;
import com.zhuss.car.db.entity.Car;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;

/**
 * @description: 车辆删除
 * @author: zhushanshuo
 * @create: Fri Dec 18 20:35:18 CST 2020
 **/
@Component("carDelete")
public class CarDelete extends AbstractProcessor<Object, DeleteCarBo> implements Processor<Object, DeleteCarBo> {

    @Autowired
    private CarDao carDao;

    @Autowired
	@Qualifier("carRead")
	private CarRead carRead;

	@Autowired
	@Qualifier("carCache")
	private CarCache carCache;


    /**
     * 校验
     */
	@Override
    public void check(DeleteCarBo bo) throws DzcxException {
		if (bo.getCarId() == null || bo.getCarId() <=0l) {
			throw new DzcxException(ExceptionCode.车辆ID为空);
		}
		if (carDao.trainByCarId(bo.getCarId()) > 0) {
			throw new DzcxException(ExceptionCode.该车辆已被班次使用);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteCarBo param) throws DzcxException {
    	Long carId = param.getCarId();
    	String name = ((DetailCarVo) carRead.detail(carId)).getPlateNo();

    	Car record = new Car();
    	record.setCarId(carId);
    	record.setDeleted(param.getDeleted());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	carDao.delete(record);
    	param.setName(name);
		carCache.clear(carId);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteCarBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.车辆.key());
    	record.setAction(LogActionEnum.车辆删除.key());
    	record.setTargetId(param.getCarId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
